package com.xilihui.xlh.business.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.alipay.AuthResult;
import com.xilihui.xlh.alipay.PayResult;
import com.xilihui.xlh.business.adapters.StoreOrderAdapter;
import com.xilihui.xlh.business.entities.PayEntity;
import com.xilihui.xlh.business.entities.PublicOrderEntity;
import com.xilihui.xlh.business.entities.StoreMyOrderEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/10.
 */

public class StoreOrderFragment extends BaseCompatFragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    ArrayList<PublicOrderEntity> datas = new ArrayList<>();
    StoreOrderAdapter baseAdapter;
    
    int page = 1;
    int status = 0;

   
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_store_order,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        YEventBuses.register(this);
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);        
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen.divider_space, LinearLayout.VERTICAL));
                
        getExtras();
        baseAdapter = new StoreOrderAdapter(getActivity(),status);
        recyclerView.setAdapter(baseAdapter);
        getData(true);
        
        
    }
    
    public void getData(boolean isLoading){
        StoreRequest.storeOrderf(getActivity(),status+"",page)
                .compose(DoTransform.<StoreMyOrderEntity>applyScheduler(getActivity(),isLoading))
                .compose(this.<StoreMyOrderEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreMyOrderEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(StoreMyOrderEntity entity) {
                    
                        if(page==1){
                            datas.clear();
                            if(entity.getOrder_all().size()>0){
                                ArrayList<PublicOrderEntity> order_all = new ArrayList<>();
                                order_all.addAll(entity.getOrder_all());
                                for (int i=0;i<order_all.size();i++){
                                    order_all.get(i).setType(1);
                                }
                                datas.addAll(0,order_all);
                            }
                        }
                        
                        for (int i=0;i<entity.getOrder().size();i++){
                            entity.getOrder().get(i).setType(2);
                        }
                        
                        datas.addAll(entity.getOrder());
                        
                        if(page>1){
                            if(status==0){
                                for (int i=0;i<entity.getOrder_all().size();i++){
                                    entity.getOrder_all().get(i).setType(1);
                                }
                                datas.addAll(entity.getOrder_all());
                            }
                        }
                        
                        
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        baseAdapter.setData(datas);
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        ToastUtil.toastShortNegative(msg);
                    }
                    
                });
    }
    
    
    private void getExtras() {
        status = getArguments().getInt("status",0);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }
        
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        LogUtil.i("mylog","status+="+status);
        if(status==3){
            if(event.is("yipingjia")||event.is("delete_cancel")){
                page = 1;
                getData(false);
            }else if(event.is(StrConst.UMENG_PUSH)){
                page = 1;
                getData(false);
            }
        }else if(status==0||status==10086){
            if(event.is("store_order_cancel")){
                page = 1;
                getData(false);
            }
        }else if(status==2||status==3){
            if(event.is("querenshouhuo")){
                page = 1;
                getData(false);
            }else if(event.is(StrConst.UMENG_PUSH)){
                page = 1;
                getData(false);
            }
        }
        
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        YEventBuses.unregister(this);
    }
    
}
